package com.taoyibao.hongcanting.ui.restaurant.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.fragment.BaseFragmentPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.dialog.DialogComment;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.BalanceModel;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.GiftInfoModel;
import com.taoyibao.hongcanting.model.GiftMode;
import com.taoyibao.hongcanting.model.UserTableModel;
import com.taoyibao.hongcanting.ui.mine.activity.RechargeActivity;
import com.taoyibao.hongcanting.ui.restaurant.area.GiftSendArea;
import com.taoyibao.hongcanting.ui.restaurant.delegate.ScreenFragmentDelegate;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 舞台弹窗中大屏的Fragment
 * @date :${DATA} 11:54
 */
public class ScreenFragment extends BaseFragmentPresenter<ScreenFragmentDelegate> implements GiftSendArea.GiftSendListener, DialogComment.DialogCommentInterfaca {

    /**
     * 当前页面数据
     */
    private GiftMode mGiftMode;
    /**
     * 底部发送模块
     */
    private GiftSendArea mGiftSendArea;
    private List<GiftInfoModel> mData;
    /**
     * 用户的余额
     */
    private double mBalance;
    private String mTableId = null;//桌位Id

    @Override
    protected Class<ScreenFragmentDelegate> getDelegateClass() {
        return ScreenFragmentDelegate.class;
    }

    /**
     * @param giftMode
     * @return
     */
    public static ScreenFragment getInstance(GiftMode giftMode) {
        ScreenFragment fragment = new ScreenFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(CodeConstant.GIFT, giftMode);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        getBalance();
        getTableStatus();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (viewDelegate != null && isVisibleToUser) {
            getBalance();
            getTableStatus();
        }
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        mGiftSendArea = viewDelegate.get(R.id.layout_send);
        mGiftSendArea.setGiftSendListener(this);
        mData = new ArrayList<>();

        mGiftMode = getArguments().getParcelable(CodeConstant.GIFT);
        if (mGiftMode != null) {
            mData.addAll(mGiftMode.info);
        }
        viewDelegate.setData(mData);
    }

    /**
     * 获取用户余额
     */
    public void getBalance() {
        JsonApiWrapper.getApiService().userBalance(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<BalanceModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<BalanceModel>>>(getContext(), false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<BalanceModel>> result) {
                        if (result.code == NetCode.HTTP_200 && result.data != null && result.data.size() > 0) {
                            BalanceModel balanceModel = result.data.get(0);
                            mBalance = balanceModel.balance + balanceModel.interests;
                            //设置显示余额
                            mGiftSendArea.setData(CodeUtils.formatAmountUnit(mBalance));
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    /**
     * 发送事件
     */
    @Override
    public void onSend() {
        GiftInfoModel chooseGift = viewDelegate.getChooseGift();
        if (!TextUtils.isEmpty(viewDelegate.getContext()) && chooseGift != null) {
            //账户余额充足
            if (mBalance >= chooseGift.price) {
                sendGift(chooseGift);
            } else {//余额不足
                DialogComment dialogComment = new DialogComment(CodeUtils.getIdString(R.string.dialog_recharge), this);
                dialogComment.showDilaog(getChildFragmentManager(), "ScreenFragment");
            }
        }
    }

    /**
     * 发送礼物接口
     */
    public void sendGift(GiftInfoModel giftInfoModel) {
        JsonApiWrapper.getApiService().sendGift(SpUtils.getToken(), giftInfoModel.id, giftInfoModel.price + "", viewDelegate.getContext(), mTableId)
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(getContext(), true) {
                    @Override
                    public void onNext(CompanyBaseModel result) {
                        if (result.code == NetCode.HTTP_200) {
                            ToastUtils.showToast(CodeUtils.getIdString(R.string.map_sendSuccess));
                            getBalance();
                            viewDelegate.setEmptyInput();
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    /**
     * 获取当前用户的就餐状态
     */
    public void getTableStatus() {
        JsonApiWrapper.getApiService().tableStatus(SpUtils.getToken())
                .compose(new DefaultTransformer<CompanyBaseModel<List<UserTableModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<UserTableModel>>>(getContext(), false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<UserTableModel>> result) {
                        if (result.code == NetCode.HTTP_200 && result.data != null && result.data.size() > 0) {
                            UserTableModel userTableModel = result.data.get(0);
                            if (userTableModel != null && !TextUtils.isEmpty(userTableModel.desk_code)) {
                                mTableId = userTableModel.desk_code;
                            }
                        } else {
                            super.onNext(result);
                        }
                    }
                });
    }

    @Override
    public void onConfirmListener() {
        startActivity(new Intent(getContext(), RechargeActivity.class));
    }
}
